#ifndef MAPWIDGET_H
#define MAPWIDGET_H

#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QPainter>
#include <QTimer>
#include <cmath>
#include <vector>

#include <QWidget>
#include <QPushButton>
#include <vector>

#include "common.h"



class MapWidget : public QWidget
{
    Q_OBJECT

public:
    MapWidget(QWidget *parent = nullptr);

    QString vison_ros_string;
    QString sql_write_position;
    void write_sql();

protected:
    void paintEvent(QPaintEvent *event) override
    {
        Q_UNUSED(event);
        QPainter painter(this);

        // 画栅格地图
        painter.setPen(Qt::darkGray);
        for (int i = 0; i < 120; i++)
        {
            for (int j = 0; j < 120; j++)
            {
                painter.drawRect(i * 5, j * 5, 5, 5);
            }
        }

        // 画车辆轨迹
        painter.setPen(Qt::blue);
        for (const auto &point : trackPoints)
        {
            painter.drawPoint(point);
        }

        // 画车辆
        painter.setPen(Qt::NoPen);
        painter.setBrush(Qt::red);
        painter.save();
        painter.translate(carX, carY);
        painter.rotate(-carDirection);
        painter.drawEllipse(-2, -2, 5, 5);
        painter.drawLine(0, 0, 10, 0);
        painter.restore();
    }

public:
    double carX;
    double carY;
    point carOldPositon;
    double initPositionX,initPositionY;
    point zuoshang,youshang,zuoxia,youxia;
    double boundXz, boundXy;
    double boundYs, boundYx;
    double carDirection;
    int samePathCount;
    int carStat;//车辆状态：0:空闲 1.耕耘  2.去耕耘  3.返回车间
    int xuangengStat;
    double engnge;//电量
    std::vector<QPoint> trackPoints;
    int visonLog;
    void updateLog(QString log);

    int carStatOld;

    QPushButton *forwardButton;
    QPushButton *backwardButton;
    QPushButton *leftButton;
    QPushButton *rightButton;
    QPushButton *stopButton;

    void moveCar();
    void startCar();
    void backward();
    void turnLeft();
    void turnRight();
    void stop();

    bool stopped;

    void gengdi();
    void qugengdi();
    void likaigengdi();
    void transzuobiao();
    void fanhuichejian();
signals:
    void mySignal(); // 声明一个信号
};

#endif // MAPWIDGET_H
